import fs from 'fs';


logger.info(logger.bgGreen.bold.yellow("character-image-plugin模块加载中..."));

// 检查全局 Redis 实例
if (!global.redis) {
    const { redis } = await import("../../lib/config/redis.js");
    global.redis = redis;
}

// 获取需要加载的应用列表
let ret = [];
let apps = {};
const files = fs
    .readdirSync(`./plugins/character-image-plugin/apps`)
    .filter((file) => file.endsWith(".js"));

files.forEach((file) => {
    ret.push(import(`./apps/${file}`));
});

// 等待所有文件加载完成
ret = await Promise.allSettled(ret);

// 处理加载结果, 保存成功加载的应用
for (let i in files) {
    let name = files[i].replace(".js", "");

    if (ret[i].status != "fulfilled") {
        logger.error(`载入插件错误：${logger.red(name)}`);
        logger.error(ret[i].reason);
        continue;
    }
    apps[name] = ret[i].value[Object.keys(ret[i].value)[0]];
}

logger.info(logger.bgGreen.bold.yellow("character-image-plugin插件加载完毕"));

export { apps };